package com.yayo.mangement.utils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;

public class HttpUtils {

    /**
     * 发送GET请求
     *
     * @param urlStr 请求的URL地址
     * @return 响应的内容
     */
    public static String sendGet(String urlStr) {
        String result = "";
        BufferedReader in = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("GET");
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestProperty("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36");
            conn.setRequestProperty("Cookie", "HMF_CI=06a19b7825f233d0568d77386d4210b98385d5f3d65fd85517ccd62b52e8e8394dc6a3d688422662e79b40ac99be97cda423a7abce8f9a2ab45f9cd920a3d91d6c");

            // 发起连接，获取HTTP响应码
            int responseCode = conn.getResponseCode();
            if (responseCode == HttpURLConnection.HTTP_OK) {
                // 读取数据
                in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
                String line;
                while ((line = in.readLine()) != null) {
                    result += line;
                }
            } else {
                throw new RuntimeException("GET请求失败: " + responseCode);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }

    /**
     * 发送POST请求
     *
     * @param urlStr 请求的URL地址
     * @param postData 发送的POST数据
     * @return 响应的内容
     */
    public static String sendPost(String urlStr, String postData) {
        String result = "";
        BufferedWriter out = null;
        BufferedReader in = null;
        try {
            URL url = new URL(urlStr);
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            conn.setRequestMethod("POST");
            conn.setDoOutput(true);
            conn.setDoInput(true);
            conn.setUseCaches(false);
            conn.setConnectTimeout(5000);
            conn.setReadTimeout(5000);
            conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");

            // 发送POST输出
            out = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
            out.write(postData);
            out.flush();

            // 读取输入流
            in = new BufferedReader(new InputStreamReader(conn.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            try {
                if (out != null) {
                    out.close();
                }
                if (in != null) {
                    in.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return result;
    }
}
